﻿using Himp.TaskScheduling.Datas;
using Himp.TaskScheduling.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace Himp.TaskScheduling.EntityFrameworkCore;

public static class TaskSchedulingDbContextModelCreatingExtensions
{
    public static void ConfigureTaskScheduling(
        this ModelBuilder builder)
    {
        Check.NotNull(builder, nameof(builder));

        /* Configure all entities here. Example:

        builder.Entity<Question>(b =>
        {
            //Configure table & schema name
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "Questions", TaskSchedulingDbProperties.DbSchema);

            b.ConfigureByConvention();

            //Properties
            b.Property(q => q.Title).IsRequired().HasMaxLength(QuestionConsts.MaxTitleLength);

            //Relations
            b.HasMany(question => question.Tags).WithOne().HasForeignKey(qt => qt.QuestionId);

            //Indexes
            b.HasIndex(q => q.CreationTime);
        });
        */

        builder.Entity<SamplePreparationTask>(b =>
        {
            //Configure table & schema name
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "SamplePreparationTasks", TaskSchedulingDbProperties.DbSchema);

            b.ConfigureByConvention();
            b.Property(q => q.Id).HasMaxLength(32);
            b.Property(q => q.SampleCode).IsRequired().HasMaxLength(32);
            b.Property(q => q.TaskAssgnTime).IsRequired();
            b.Property(q => q.SampeWay).HasMaxLength(16);
            b.Property(q => q.ExecStas).IsRequired().HasMaxLength(16);
        });

        builder.Entity<StandardTestTask>(b =>
        {
            //Configure table & schema name
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "StandardTestTasks", TaskSchedulingDbProperties.DbSchema);
            b.Property(e => e.Id).ValueGeneratedOnAdd();
            b.ConfigureByConvention();

            b.Property(q => q.SampleCode).IsRequired().HasMaxLength(32);
            b.Property(q => q.TestCode).IsRequired().HasMaxLength(32);
            b.Property(q => q.BottomCode).IsRequired().HasMaxLength(32);
            b.Property(q => q.Sampletype).HasMaxLength(16);
            b.Property(q => q.TaskAssgnTime).IsRequired();
            b.Property(q => q.LastOperTime).IsRequired();
            b.Property(q => q.ExecStas).IsRequired().HasMaxLength(16);
        });

        builder.Entity<SampleTestTask>(b =>
        {
            //Configure table & schema name
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "SampleTestTasks", TaskSchedulingDbProperties.DbSchema);
            
            b.ConfigureByConvention();
            b.Property(e => e.Id).ValueGeneratedOnAdd();
            b.Property(q => q.SampleCode).IsRequired().HasMaxLength(32);
            b.Property(q => q.TestCode).IsRequired().HasMaxLength(32);
            b.Property(q => q.BottomCode).HasMaxLength(32);
            b.Property(q => q.SampleType).HasMaxLength(16);
            b.Property(q => q.TaskAssgnTime).IsRequired();
            b.Property(q => q.LastOperTime).IsRequired();
            b.Property(q => q.ExecStas).IsRequired().HasMaxLength(16);
        });

        builder.Entity<BizflowConfig>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "BizflowConfig", TaskSchedulingDbProperties.DbSchema);

            b.ConfigureByConvention();
        });

        builder.Entity<BizflowNode>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "BizflowNode", TaskSchedulingDbProperties.DbSchema);

            b.ConfigureByConvention();
        });

        builder.Entity<SampleObtainTask>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "SampleObtainTask", TaskSchedulingDbProperties.DbSchema);
            b.Property(e => e.Id).ValueGeneratedOnAdd();

            b.ConfigureByConvention();
        });

        builder.Entity<SamplePreparationResult>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "SamplePreparationResult", TaskSchedulingDbProperties.DbSchema);

            b.ConfigureByConvention();
        });

        builder.Entity<BizflowNodeAssocCandidateUser>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "BizflowNodeAssocCandidateUser", TaskSchedulingDbProperties.DbSchema);
            b.ConfigureByConvention();
        });

        builder.Entity<ParamConfigTask>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "ParamConfigTask", TaskSchedulingDbProperties.DbSchema);
            b.Property(e => e.Id).ValueGeneratedOnAdd();
            b.ConfigureByConvention();
        });

        builder.Entity<ParamConfig>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "ParamConfig", TaskSchedulingDbProperties.DbSchema);
            b.Property(e => e.Id).ValueGeneratedOnAdd();
            b.ConfigureByConvention();
        });

        builder.Entity<SamplePreparationPlan>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "SamplePreparationPlan", TaskSchedulingDbProperties.DbSchema);

            b.HasKey(oi => new { oi.Id, oi.ParName });
            b.ConfigureByConvention();
        });

        builder.Entity<StandardTestItem>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "StandardTestItem", TaskSchedulingDbProperties.DbSchema);

            b.HasKey(oi => new { oi.Id, oi.TestItems });
            b.ConfigureByConvention();
        });

        builder.Entity<EventMsgRec>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "EventMsgRec", TaskSchedulingDbProperties.DbSchema);

            b.ConfigureByConvention();
        });

        builder.Entity<EventMsgTopic>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "EventMsgTopic", TaskSchedulingDbProperties.DbSchema);

            b.ConfigureByConvention();
        });

        builder.Entity<CodeConversion>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "CodeConversion", TaskSchedulingDbProperties.DbSchema);
            b.Property(e => e.Id).ValueGeneratedOnAdd();

            b.ConfigureByConvention();
        });

        builder.Entity<CmDeviceControl>(b =>
        {
            b.ToTable("CM_DEVICE_CONTROL", TaskSchedulingDbProperties.DbSchema);
            b.Property(e => e.Id).ValueGeneratedOnAdd();

            b.ConfigureByConvention();
        });

        builder.Entity<ZYOperateRecord>(b =>
        {
            b.ToTable(TaskSchedulingDbProperties.DbTablePrefix + "ZYOperateRecord", TaskSchedulingDbProperties.DbSchema);

            b.ConfigureByConvention();
        });
    }
}
